package com.threeonemeeting.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.mobclick.android.MobclickAgent;
import com.mobclick.android.ReportPolicy;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.tab.ScrollableTabActivity;

public class MainActivity extends ScrollableTabActivity {

	public static final int TAB_INDEX_HOME = 0;
	public static final int TAB_INDEX_CATEGORY = 1;
	public static final int TAB_INDEX_MY31 = 2;
	public static final int TAB_INDEX_MESSAGE = 3;
	public static final int TAB_INDEX_MORE = 4;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		MobclickAgent.onError(this);
		MobclickAgent.update(this);
		MobclickAgent.setAutoLocation(false);
		MobclickAgent.setUpdateOnlyWifi(false);
		MobclickAgent.update(this, 1000 * 60 * 60 * 24);// daily
		MobclickAgent
				.setDefaultReportPolicy(this, ReportPolicy.BATCH_AT_LAUNCH);

		/*
		 * set this activity as the tab bar delegate so that onTabChanged is
		 * called when users tap on the bar
		 */
		setDelegate(new SliderBarActivityDelegateImpl());

		Intent homeIntent = new Intent(this, TabHomeActivity.class);
		this.addTab("", R.drawable.home, R.drawable.home_focus, homeIntent);

		Intent categoryIntent = new Intent(this, TabTradesActivity.class);
		this.addTab("", R.drawable.cate, R.drawable.cate_focus, categoryIntent);

		Intent my31Intent = new Intent(this, TabMyMeetingActivity.class);
		this.addTab("", R.drawable.my31, R.drawable.my31_focus, my31Intent);

		Intent messageIntent = new Intent(this, TabMessageActivity.class);
		this.addTab("", R.drawable.message, R.drawable.message_focus,
				messageIntent);

		Intent moreIntent = new Intent(this, TabMoreActivity.class);
		this.addTab("", R.drawable.more, R.drawable.more_focus, moreIntent);

		/*
		 * commit is required to redraw the bar after add tabs are added if you
		 * know of a better way, drop me your suggestion please.
		 */
		commit();

		DataCenter.getInstance().init(this);

	}

	private class SliderBarActivityDelegateImpl extends
			SliderBarActivityDelegate {
		/*
		 * Optional callback method called when users tap on the tab bar button
		 */
		protected void onTabChanged(int tabIndex) {
			Log.d("onTabChanged", "" + tabIndex);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

			// 检查能否callBack
			if (canBack()) {
				onBack();
			} else {
				exitConfirmDialog();
			}

			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	}

	private boolean canBack() {
		MultiActivityTab multiTab = (MultiActivityTab) this
				.getCurrentActivity();
		if (multiTab != null) {
			return multiTab.canBack();
		}

		return false;
	}

	// 回退到上一个Activity
	private void onBack() {
		MultiActivityTab multiTab = (MultiActivityTab) this.activityManager
				.getCurrentActivity();
		if (multiTab != null) {
			multiTab.back();
		}
	}

	protected void exitConfirmDialog() {

		String appName = getResources().getString(R.string.app_name);

		DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				android.os.Process.killProcess(android.os.Process.myPid());
			}

		};

		MessageBox msgBox = new MessageBox(this);
		msgBox.showDialog("提示", "确定要退出" + appName + "吗?", confirmListener);
	}

	@Override
	public void onNewIntent(Intent intent) {
		Log.d(Const.Tag, "helloworld");
		Bundle extras = intent.getExtras();
		if (extras == null)
			return;

		String strDest = extras.getString("dest");
		if (strDest == null)
			return;

		Activity ac = this.getCurrentActivity();
		MultiActivityTab multiActivity = (MultiActivityTab) ac;
		if (multiActivity == null)
			return;

		BaseTabActivity tabActivity = (BaseTabActivity) multiActivity
				.getCurrentActivity();
		if (tabActivity == null)
			return;

		Bundle b = intent.getExtras();
		if (b == null)
			b = new Bundle();

		b.putString("uri", intent.getDataString());
		tabActivity.startActivity(strDest, b);
	}
}
